home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perlre.Z / perlre
Encoding:
Text File  |  1998-10-28  |  48.6 KB  |  1,387 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perlre - Perl    regular    expressions
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.       This page describes the syntax of regular expressions    in
  13.       Perl.     For a description of how to _u_s_e regular expressions
  14.       in matching operations, plus various examples    of the same,
  15.       see discussion of m//, s///, qr// and    ?? in the section on
  16.       _R_e_g_e_x_p _Q_u_o_t_e-_L_i_k_e _O_p_e_r_a_t_o_r_s in the _p_e_r_l_o_p manpage.
  17.  
  18.       The matching operations can have various modifiers.  The
  19.       modifiers that relate    to the interpretation of the regular
  20.       expression inside are    listed below.  For the modifiers that
  21.       alter    the way    a regular expression is    used by    Perl, see the
  22.       section on _R_e_g_e_x_p _Q_u_o_t_e-_L_i_k_e _O_p_e_r_a_t_o_r_s in the    _p_e_r_l_o_p manpage
  23.       and the section on _G_o_r_y _d_e_t_a_i_l_s _o_f _p_a_r_s_i_n_g _q_u_o_t_e_d _c_o_n_s_t_r_u_c_t_s
  24.       in the _p_e_r_l_o_p    manpage.
  25.  
  26.       i   Do case-insensitive pattern matching.
  27.  
  28.           If use locale is in effect, the case map is taken    from
  29.           the current locale.  See the _p_e_r_l_l_o_c_a_l_e manpage.
  30.  
  31.       m   Treat string as multiple lines.  That is,    change "^" and
  32.           "$" from matching    at only    the very start or end of the
  33.           string to    the start or end of any    line anywhere within
  34.           the string,
  35.  
  36.       s   Treat string as single line.  That is, change "."    to
  37.           match any    character whatsoever, even a newline, which it
  38.           normally would not match.
  39.  
  40.           The /s and /m modifiers both override the    $* setting.
  41.           That is, no matter what $* contains, /s without /m will
  42.           force "^"    to match only at the beginning of the string
  43.           and "$" to match only at the end (or just    before a
  44.           newline at the end) of the string.  Together, as /ms,
  45.           they let the "." match any character whatsoever, while
  46.           yet allowing "^" and "$" to match, respectively, just
  47.           after and    just before newlines within the    string.
  48.  
  49.       x   Extend your pattern's legibility by permitting
  50.           whitespace and comments.
  51.  
  52.       These    are usually written as "the /x modifier", even though
  53.       the delimiter    in question might not actually be a slash.  In
  54.       fact,    any of these modifiers may also    be embedded within the
  55.       regular expression itself using the new (?...) construct.
  56.       See below.
  57.  
  58.       The /x modifier itself needs a little    more explanation.  It
  59.       tells    the regular expression parser to ignore    whitespace
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  71.  
  72.  
  73.  
  74.       that is neither backslashed nor within a character class.
  75.       You can use this to break up your regular expression into
  76.       (slightly) more readable parts.  The # character is also
  77.       treated as a metacharacter introducing a comment, just as in
  78.       ordinary Perl    code.  This also means that if you want    real
  79.       whitespace or    # characters in    the pattern (outside of    a
  80.       character class, where they are unaffected by    /x), that
  81.       you'll either    have to    escape them or encode them using octal
  82.       or hex escapes.  Taken together, these features go a long
  83.       way towards making Perl's regular expressions    more readable.
  84.       Note that you    have to    be careful not to include the pattern
  85.       delimiter in the comment--perl has no    way of knowing you did
  86.       not intend to    close the pattern early.  See the C-comment
  87.       deletion code    in the _p_e_r_l_o_p manpage.
  88.  
  89.       RRRReeeegggguuuullllaaaarrrr EEEExxxxpppprrrreeeessssssssiiiioooonnnnssss
  90.  
  91.       The patterns used in pattern matching    are regular
  92.       expressions such as those supplied in    the Version 8 regex
  93.       routines.  (In fact, the routines are    derived    (distantly)
  94.       from Henry Spencer's freely redistributable reimplementation
  95.       of the V8 routines.)    See the    section    on _V_e_r_s_i_o_n _8 _R_e_g_u_l_a_r
  96.       _E_x_p_r_e_s_s_i_o_n_s for details.
  97.  
  98.       In particular    the following metacharacters have their
  99.       standard _e_g_r_e_p-ish meanings:
  100.  
  101.           \      Quote    the next metacharacter
  102.           ^      Match    the beginning of the line
  103.           .      Match    any character (except newline)
  104.           $      Match    the end    of the line (or    before newline at the end)
  105.           |      Alternation
  106.           ()  Grouping
  107.           []  Character class
  108.  
  109.       By default, the "^" character    is guaranteed to match at only
  110.       the beginning    of the string, the "$" character at only the
  111.       end (or before the newline at    the end) and Perl does certain
  112.       optimizations    with the assumption that the string contains
  113.       only one line.  Embedded newlines will not be    matched    by "^"
  114.       or "$".  You may, however, wish to treat a string as a
  115.       multi-line buffer, such that the "^" will match after    any
  116.       newline within the string, and "$" will match    before any
  117.       newline.  At the cost    of a little more overhead, you can do
  118.       this by using    the /m modifier    on the pattern match operator.
  119.       (Older programs did this by setting $*, but this practice is
  120.       now deprecated.)
  121.  
  122.       To facilitate    multi-line substitutions, the "." character
  123.       never    matches    a newline unless you use the /s    modifier,
  124.       which    in effect tells    Perl to    pretend    the string is a    single
  125.       line--even if    it isn't.  The /s modifier also    overrides the
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  137.  
  138.  
  139.  
  140.       setting of $*, in case you have some (badly behaved) older
  141.       code that sets it in another module.
  142.  
  143.       The following    standard quantifiers are recognized:
  144.  
  145.           *         Match 0 or    more times
  146.           +         Match 1 or    more times
  147.           ?         Match 1 or    0 times
  148.           {n}    Match exactly n times
  149.           {n,}   Match at least n times
  150.           {n,m}  Match at least n but not more than    m times
  151.  
  152.       (If a    curly bracket occurs in    any other context, it is
  153.       treated as a regular character.)  The    "*" modifier is
  154.       equivalent to    {0,}, the "+" modifier to {1,},    and the    "?"
  155.       modifier to {0,1}.  n    and m are limited to integral values
  156.       less than 65536.
  157.  
  158.       By default, a    quantified subpattern is "greedy", that    is, it
  159.       will match as    many times as possible (given a    particular
  160.       starting location) while still allowing the rest of the
  161.       pattern to match.  If    you want it to match the minimum
  162.       number of times possible, follow the quantifier with a "?".
  163.       Note that the    meanings don't change, just the    "greediness":
  164.  
  165.           *?     Match 0 or    more times
  166.           +?     Match 1 or    more times
  167.           ??     Match 0 or    1 time
  168.           {n}?   Match exactly n times
  169.           {n,}?  Match at least n times
  170.           {n,m}? Match at least n but not more than    m times
  171.  
  172.       Because patterns are processed as double quoted strings, the
  173.       following also work:
  174.  
  175.           \t      tab            (HT, TAB)
  176.           \n      newline        (LF, NL)
  177.           \r      return        (CR)
  178.           \f      form feed        (FF)
  179.           \a      alarm    (bell)        (BEL)
  180.           \e      escape (think    troff)    (ESC)
  181.           \033      octal    char (think of a PDP-11)
  182.           \x1B      hex char
  183.           \c[      control char
  184.           \l      lowercase next char (think vi)
  185.           \u      uppercase next char (think vi)
  186.           \L      lowercase till \E (think vi)
  187.           \U      uppercase till \E (think vi)
  188.           \E      end case modification    (think vi)
  189.           \Q      quote    (disable) pattern metacharacters till \E
  190.  
  191.       If use locale    is in effect, the case map used    by \l, \L, \u
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  203.  
  204.  
  205.  
  206.       and \U is taken from the current locale.  See    the _p_e_r_l_l_o_c_a_l_e
  207.       manpage.
  208.  
  209.       You cannot include a literal $ or @ within a \Q sequence.
  210.       An unescaped $ or @ interpolates the corresponding variable,
  211.       while    escaping will cause the    literal    string \$ to be
  212.       matched.  You'll need    to write something like
  213.       m/\Quser\E\@\Qhost/.
  214.  
  215.       In addition, Perl defines the    following:
  216.  
  217.           \w  Match    a "word" character (alphanumeric plus "_")
  218.           \W  Match    a non-word character
  219.           \s  Match    a whitespace character
  220.           \S  Match    a non-whitespace character
  221.           \d  Match    a digit    character
  222.           \D  Match    a non-digit character
  223.  
  224.       A \w matches a single    alphanumeric character,    not a whole
  225.       word.     To match a word you'd need to say \w+.     If use    locale
  226.       is in    effect,    the list of alphabetic characters generated by
  227.       \w is    taken from the current locale.    See the    _p_e_r_l_l_o_c_a_l_e
  228.       manpage. You may use \w, \W, \s, \S, \d, and \D within
  229.       character classes (though not    as either end of a range).
  230.  
  231.       Perl defines the following zero-width    assertions:
  232.  
  233.           \b  Match    a word boundary
  234.           \B  Match    a non-(word boundary)
  235.           \A  Match    only at    beginning of string
  236.           \Z  Match    only at    end of string, or before newline at the    end
  237.           \z  Match    only at    end of string
  238.           \G  Match    only where previous m//g left off (works only with /g)
  239.  
  240.       A word boundary (\b) is defined as a spot between two
  241.       characters that has a    \w on one side of it and a \W on the
  242.       other    side of    it (in either order), counting the imaginary
  243.       characters off the beginning and end of the string as
  244.       matching a \W.  (Within character classes \b represents
  245.       backspace rather than    a word boundary.)  The \A and \Z are
  246.       just like "^"    and "$", except    that they won't    match multiple
  247.       times    when the /m modifier is    used, while "^"    and "$"    will
  248.       match    at every internal line boundary.  To match the actual
  249.       end of the string, not ignoring newline, you can use \z.
  250.       The \G assertion can be used to chain    global matches (using
  251.       m//g), as described in the section on    _R_e_g_e_x_p _Q_u_o_t_e-_L_i_k_e
  252.       _O_p_e_r_a_t_o_r_s in the _p_e_r_l_o_p manpage.
  253.  
  254.       It is    also useful when writing lex-like scanners, when you
  255.       have several patterns    that you want to match against
  256.       consequent substrings    of your    string,    see the    previous
  257.       reference.  The actual location where    \G will    match can also
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  269.  
  270.  
  271.  
  272.       be influenced    by using pos() as an lvalue.  See the pos
  273.       entry    in the _p_e_r_l_f_u_n_c    manpage.
  274.  
  275.       When the bracketing construct    ( ... )    is used, \<digit>
  276.       matches the digit'th substring.  Outside of the pattern,
  277.       always use "$" instead of "\"    in front of the    digit.    (While
  278.       the \<digit> notation    can on rare occasion work outside the
  279.       current pattern, this    should not be relied upon.  See    the
  280.       WARNING below.) The scope of $<digit>    (and $`, $&, and $')
  281.       extends to the end of    the enclosing BLOCK or eval string, or
  282.       to the next successful pattern match,    whichever comes    first.
  283.       If you want to use parentheses to delimit a subpattern
  284.       (e.g., a set of alternatives)    without    saving it as a
  285.       subpattern, follow the ( with    a ?:.
  286.  
  287.       You may have as many parentheses as you wish.     If you    have
  288.       more than 9 substrings, the variables    $10, $11, ... refer to
  289.       the corresponding substring.    Within the pattern, \10, \11,
  290.       etc. refer back to substrings    if there have been at least
  291.       that many left parentheses before the    backreference.
  292.       Otherwise (for backward compatibility) \10 is    the same as
  293.       \010,    a backspace, and \11 the same as \011, a tab.  And so
  294.       on.  (\1 through \9 are always backreferences.)
  295.  
  296.       $+ returns whatever the last bracket match matched.  $&
  297.       returns the entire matched string.  ($0 used to return the
  298.       same thing, but not any more.)  $` returns everything    before
  299.       the matched string.  $' returns everything after the matched
  300.       string.  Examples:
  301.  
  302.           s/^([^ ]*) *([^ ]*)/$2 $1/;     #    swap first two words
  303.  
  304.           if (/Time: (..):(..):(..)/) {
  305.           $hours = $1;
  306.           $minutes = $2;
  307.           $seconds = $3;
  308.           }
  309.  
  310.       Once perl sees that you need one of $&, $` or    $' anywhere in
  311.       the program, it has to provide them on each and every
  312.       pattern match.  This can slow    your program down.  The    same
  313.       mechanism that handles these provides    for the    use of $1, $2,
  314.       etc.,    so you pay the same price for each pattern that
  315.       contains capturing parentheses. But if you never use $&,
  316.       etc.,    in your    script,    then patterns _w_i_t_h_o_u_t capturing
  317.       parentheses won't be penalized. So avoid $&, $', and $` if
  318.       you can, but if you can't (and some algorithms really
  319.       appreciate them), once you've    used them once,    use them at
  320.       will,    because    you've already paid the    price.    As of 5.005,
  321.       $& is    not so costly as the other two.
  322.  
  323.       Backslashed metacharacters in    Perl are alphanumeric, such as
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  335.  
  336.  
  337.  
  338.       \b, \w, \n.  Unlike some other regular expression languages,
  339.       there    are no backslashed symbols that    aren't alphanumeric.
  340.       So anything that looks like \\, \(, \), \<, \>, \{, or \} is
  341.       always interpreted as    a literal character, not a
  342.       metacharacter.  This was once    used in    a common idiom to
  343.       disable or quote the special meanings    of regular expression
  344.       metacharacters in a string that you want to use for a
  345.       pattern. Simply quote    all non-alphanumeric characters:
  346.  
  347.           $pattern =~ s/(\W)/\\$1/g;
  348.  
  349.       Now it is much more common to    see either the _q_u_o_t_e_m_e_t_a()
  350.       function or the \Q escape sequence used to disable all
  351.       metacharacters' special meanings like    this:
  352.  
  353.           /$unquoted\Q$quoted\E$unquoted/
  354.  
  355.       Perl defines a consistent extension syntax for regular
  356.       expressions.    The syntax is a    pair of    parentheses with a
  357.       question mark    as the first thing within the parentheses
  358.       (this    was a syntax error in older versions of    Perl).    The
  359.       character after the question mark gives the function of the
  360.       extension.  Several extensions are already supported:
  361.  
  362.       (?#text)  A comment.    The text is ignored.  If the /x    switch
  363.             is used to enable whitespace formatting, a simple
  364.             # will suffice.  Note that perl closes the comment
  365.             as soon as it sees a ), so there is    no way to put
  366.             a literal )    in the comment.
  367.  
  368.       (?:pattern)
  369.  
  370.       (?imsx-imsx:pattern)
  371.             This is for    clustering, not    capturing; it groups
  372.             subexpressions like    "()", but doesn't make
  373.             backreferences as "()" does.  So
  374.  
  375.             @fields    = split(/\b(?:a|b|c)\b/)
  376.  
  377.             is like
  378.  
  379.             @fields    = split(/\b(a|b|c)\b/)
  380.  
  381.             but    doesn't    spit out extra fields.
  382.  
  383.             The    letters    between    ? and :    act as flags
  384.             modifiers, see the (?_i_m_s_x-_i_m_s_x) manpage.  In
  385.             particular,
  386.  
  387.             /(?s-i:more.*than).*million/i
  388.  
  389.             is equivalent to more verbose
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  401.  
  402.  
  403.  
  404.             /(?:(?s-i)more.*than).*million/i
  405.  
  406.  
  407.       (?=pattern)
  408.             A zero-width positive lookahead assertion.    For
  409.             example, /\w+(?=\t)/ matches a word    followed by a
  410.             tab, without including the tab in $&.
  411.  
  412.       (?!pattern)
  413.             A zero-width negative lookahead assertion.    For
  414.             example /foo(?!bar)/ matches any occurrence    of
  415.             "foo" that isn't followed by "bar".     Note however
  416.             that lookahead and lookbehind are NOT the same
  417.             thing.  You    cannot use this    for lookbehind.
  418.  
  419.             If you are looking for a "bar" that    isn't preceded
  420.             by a "foo",    /(?!foo)bar/ will not do what you
  421.             want.  That's because the (?!foo) is just saying
  422.             that the next thing    cannot be "foo"--and it's not,
  423.             it's a "bar", so "foobar" will match.  You would
  424.             have to do something like /(?!foo)...bar/ for
  425.             that.   We say "like" because there's the case of
  426.             your "bar" not having three    characters before it.
  427.             You    could cover that this way:
  428.             /(?:(?!foo)...|^.{0,2})bar/.  Sometimes it's still
  429.             easier just    to say:
  430.  
  431.             if (/bar/ && $`    !~ /foo$/)
  432.  
  433.             For    lookbehind see below.
  434.  
  435.       (?<=pattern)
  436.             A zero-width positive lookbehind assertion.     For
  437.             example, /(?<=\t)\w+/ matches a word following a
  438.             tab, without including the tab in $&.  Works only
  439.             for    fixed-width lookbehind.
  440.  
  441.       (?<!pattern)
  442.             A zero-width negative lookbehind assertion.     For
  443.             example /(?<!bar)foo/ matches any occurrence of
  444.             "foo" that isn't following "bar". Works only for
  445.             fixed-width    lookbehind.
  446.  
  447.       (?{ code })
  448.             Experimental "evaluate any Perl code" zero-width
  449.             assertion.    Always succeeds.  code is not
  450.             interpolated.  Currently the rules to determine
  451.             where the code ends    are somewhat convoluted.
  452.  
  453.             The    code is    properly scoped    in the following
  454.             sense: if the assertion is backtracked (compare
  455.             the    section    on _B_a_c_k_t_r_a_c_k_i_n_g), all the changes
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  467.  
  468.  
  469.  
  470.             introduced after localisation are undone, so
  471.  
  472.               $_ = 'a' x 8;
  473.               m<
  474.              (?{ $cnt = 0 })            # Initialize $cnt.
  475.              (
  476.                a
  477.                (?{
  478.                    local $cnt = $cnt + 1;        # Update $cnt, backtracking-safe.
  479.                })
  480.              )*
  481.              aaaa
  482.              (?{ $res = $cnt })            # On success copy to non-localized
  483.                                 # location.
  484.                >x;
  485.  
  486.             will set $res = 4.    Note that after    the match $cnt
  487.             returns to the globally introduced value 0,    since
  488.             the    scopes which restrict local statements are
  489.             unwound.
  490.  
  491.             This assertion may be used as (?(condition)yes-
  492.             pattern switch.  If    _n_o_t used in this way, the
  493.             result of evaluation of code is put    into variable
  494.             $^R.  This happens immediately, so $^R can be used
  495.             from other (?{ code    }) assertions inside the same
  496.             regular expression.
  497.  
  498.             The    above assignment to $^R    is properly localized,
  499.             thus the old value of $^R is restored if the
  500.             assertion is backtracked (compare the section on
  501.             _B_a_c_k_t_r_a_c_k_i_n_g).
  502.  
  503.             Due    to security concerns, this construction    is not
  504.             allowed if the regular expression involves run-
  505.             time interpolation of variables, unless use    re
  506.             'eval' pragma is used (see the _r_e manpage),    or the
  507.             variables contain results of _q_r() operator (see
  508.             the    section    on _q_r/_S_T_R_I_N_G/_i_m_o_s_x in the _p_e_r_l_o_p
  509.             manpage).
  510.  
  511.             This restriction is    due to the wide-spread
  512.             (questionable) practice of using the construct
  513.  
  514.             $re = <>;
  515.             chomp $re;
  516.             $string    =~ /$re/;
  517.  
  518.             without tainting.  While this code is frowned upon
  519.             from security point    of view, when (?{}) was
  520.             introduced,    it was considered bad to add _n_e_w
  521.             security holes to existing scripts.
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  533.  
  534.  
  535.  
  536.             NNNNOOOOTTTTEEEE::::  Use of the above insecure snippet without
  537.             also enabling taint    mode is    to be severely frowned
  538.             upon.  use re 'eval' does not disable tainting
  539.             checks, thus to allow $re in the above snippet to
  540.             contain (?{}) _w_i_t_h _t_a_i_n_t_i_n_g    _e_n_a_b_l_e_d, one needs
  541.             both use re    'eval' and untaint the $re.
  542.  
  543.       (?>pattern)
  544.             An "independent" subexpression.  Matches the
  545.             substring that a _s_t_a_n_d_a_l_o_n_e    pattern    would match if
  546.             anchored at    the given position, aaaannnndddd    oooonnnnllllyyyy tttthhhhiiiissss
  547.             ssssuuuubbbbssssttttrrrriiiinnnngggg.
  548.  
  549.             Say, ^(?>a*)ab will    never match, since (?>a*)
  550.             (anchored at the beginning of string, as above)
  551.             will match _a_l_l characters a    at the beginning of
  552.             string, leaving no a for ab    to match.  In
  553.             contrast, a*ab will    match the same as a+b, since
  554.             the    match of the subgroup a* is influenced by the
  555.             following group ab (see the    section    on
  556.             _B_a_c_k_t_r_a_c_k_i_n_g).  In particular, a* inside a*ab will
  557.             match fewer    characters than    a standalone a*, since
  558.             this makes the tail    match.
  559.  
  560.             An effect similar to (?>pattern) may be achieved
  561.             by
  562.  
  563.                (?=(pattern))\1
  564.  
  565.             since the lookahead    is in "_l_o_g_i_c_a_l"    context, thus
  566.             matches the    same substring as a standalone a+.
  567.             The    following \1 eats the matched string, thus
  568.             making a zero-length assertion into    an analogue of
  569.             (?>...).  (The difference between these two
  570.             constructs is that the second one uses a catching
  571.             group, thus    shifting ordinals of backreferences in
  572.             the    rest of    a regular expression.)
  573.  
  574.             This construct is useful for optimizations of
  575.             "eternal" matches, because it will not backtrack
  576.             (see the section on    _B_a_c_k_t_r_a_c_k_i_n_g).
  577.  
  578.             m{ \(
  579.                   (
  580.                 [^()]+
  581.                   |
  582.                 \( [^()]* \)
  583.                   )+
  584.                \)
  585.              }x
  586.  
  587.             That will efficiently match    a nonempty group with
  588.  
  589.  
  590.  
  591.      Page 9                        (printed 10/23/98)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  599.  
  600.  
  601.  
  602.             matching two-or-less-level-deep parentheses.
  603.             However, if    there is no such group,    it will    take
  604.             virtually forever on a long    string.     That's
  605.             because there are so many different    ways to    split
  606.             a long string into several substrings.  This is
  607.             what (.+)+ is doing, and (.+)+ is similar to a
  608.             subpattern of the above pattern.  Consider that
  609.             the    above pattern detects no-match on
  610.             ((()aaaaaaaaaaaaaaaaaa in several seconds, but
  611.             that  each extra letter doubles this time.    This
  612.             exponential    performance will make it appear    that
  613.             your program has hung.
  614.  
  615.             However, a tiny modification of this pattern
  616.  
  617.             m{ \(
  618.                   (
  619.                 (?> [^()]+ )
  620.                   |
  621.                 \( [^()]* \)
  622.                   )+
  623.                \)
  624.              }x
  625.  
  626.             which uses (?>...) matches exactly when the    one
  627.             above does (verifying this yourself    would be a
  628.             productive exercise), but finishes in a fourth the
  629.             time when used on a    similar    string with 1000000
  630.             as.     Be aware, however, that this pattern
  631.             currently triggers a warning message under ----wwww
  632.             saying it "matches the null    string many times"):
  633.  
  634.             On simple groups, such as the pattern (? [^()]+
  635.             )>,    a comparable effect may    be achieved by
  636.             negative lookahead,    as in [^()]+ (?! [^()] ).
  637.             This was only 4 times slower on a string with
  638.             1000000 as.
  639.  
  640.       (?(condition)yes-pattern|no-pattern)
  641.  
  642.       (?(condition)yes-pattern)
  643.             Conditional    expression.  (condition) should    be
  644.             either an integer in parentheses (which is valid
  645.             if the corresponding pair of parentheses matched),
  646.             or lookahead/lookbehind/evaluate zero-width
  647.             assertion.
  648.  
  649.             Say,
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.      Page 10                        (printed 10/23/98)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  665.  
  666.  
  667.  
  668.             m{ ( \(    )?
  669.                [^()]+
  670.                (?(1) \) )
  671.              }x
  672.  
  673.             matches a chunk of non-parentheses,    possibly
  674.             included in    parentheses themselves.
  675.  
  676.       (?imsx-imsx)
  677.             One    or more    embedded pattern-match modifiers.
  678.             This is particularly useful    for patterns that are
  679.             specified in a table somewhere, some of which want
  680.             to be case sensitive, and some of which don't.
  681.             The    case insensitive ones need to include merely
  682.             (?i) at the    front of the pattern.  For example:
  683.  
  684.             $pattern = "foobar";
  685.             if ( /$pattern/i ) { }
  686.  
  687.             # more flexible:
  688.  
  689.             $pattern = "(?i)foobar";
  690.             if ( /$pattern/    ) { }
  691.  
  692.             Letters after - switch modifiers off.
  693.  
  694.             These modifiers are    localized inside an enclosing
  695.             group (if any).  Say,
  696.  
  697.             ( (?i) blah ) \s+ \1
  698.  
  699.             (assuming x    modifier, and no i modifier outside of
  700.             this group)    will match a repeated (_i_n_c_l_u_d_i_n_g _t_h_e
  701.             _c_a_s_e!) word    blah in    any case.
  702.  
  703.       A question mark was chosen for this and for the new
  704.       minimal-matching construct because 1)    question mark is
  705.       pretty rare in older regular expressions, and    2) whenever
  706.       you see one, you should stop and "question" exactly what is
  707.       going    on.  That's psychology...
  708.  
  709.       BBBBaaaacccckkkkttttrrrraaaacccckkkkiiiinnnngggg
  710.  
  711.       A fundamental    feature    of regular expression matching
  712.       involves the notion called _b_a_c_k_t_r_a_c_k_i_n_g, which is currently
  713.       used (when needed) by    all regular expression quantifiers,
  714.       namely *, *?,    +, +?, {n,m}, and {n,m}?.
  715.  
  716.       For a    regular    expression to match, the _e_n_t_i_r_e    regular
  717.       expression must match, not just part of it.  So if the
  718.       beginning of a pattern containing a quantifier succeeds in a
  719.       way that causes later    parts in the pattern to    fail, the
  720.  
  721.  
  722.  
  723.      Page 11                        (printed 10/23/98)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  731.  
  732.  
  733.  
  734.       matching engine backs    up and recalculates the    beginning
  735.       part--that's why it's    called backtracking.
  736.  
  737.       Here is an example of    backtracking:  Let's say you want to
  738.       find the word    following "foo"    in the string "Food is on the
  739.       foo table.":
  740.  
  741.           $_ = "Food is on the foo table.";
  742.           if ( /\b(foo)\s+(\w+)/i )    {
  743.           print    "$2 follows $1.\n";
  744.           }
  745.  
  746.       When the match runs, the first part of the regular
  747.       expression (\b(foo)) finds a possible    match right at the
  748.       beginning of the string, and loads up    $1 with    "Foo".
  749.       However, as soon as the matching engine sees that there's no
  750.       whitespace following the "Foo" that it had saved in $1, it
  751.       realizes its mistake and starts over again one character
  752.       after    where it had the tentative match.  This    time it    goes
  753.       all the way until the    next occurrence    of "foo". The complete
  754.       regular expression matches this time,    and you    get the
  755.       expected output of "table follows foo."
  756.  
  757.       Sometimes minimal matching can help a    lot.  Imagine you'd
  758.       like to match    everything between "foo" and "bar".
  759.       Initially, you write something like this:
  760.  
  761.           $_ =  "The food is under the bar in the barn.";
  762.           if ( /foo(.*)bar/    ) {
  763.           print    "got <$1>\n";
  764.           }
  765.  
  766.       Which    perhaps    unexpectedly yields:
  767.  
  768.         got    <d is under the    bar in the >
  769.  
  770.       That's because .* was    greedy,    so you get everything between
  771.       the _f_i_r_s_t "foo" and the _l_a_s_t "bar".  In this case, it's more
  772.       effective to use minimal matching to make sure you get the
  773.       text between a "foo" and the first "bar" thereafter.
  774.  
  775.           if ( /foo(.*?)bar/ ) { print "got    <$1>\n"    }
  776.         got    <d is under the    >
  777.  
  778.       Here's another example: let's    say you'd like to match    a
  779.       number at the    end of a string, and you also want to keep the
  780.       preceding part the match.  So    you write this:
  781.  
  782.           $_ = "I have 2 numbers: 53147";
  783.           if ( /(.*)(\d*)/ ) {                  # Wrong!
  784.           print    "Beginning is <$1>, number is <$2>.\n";
  785.           }
  786.  
  787.  
  788.  
  789.      Page 12                        (printed 10/23/98)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  797.  
  798.  
  799.  
  800.       That won't work at all, because .* was greedy    and gobbled up
  801.       the whole string. As \d* can match on    an empty string    the
  802.       complete regular expression matched successfully.
  803.  
  804.           Beginning    is <I have 2 numbers: 53147>, number is    <>.
  805.  
  806.       Here are some    variants, most of which    don't work:
  807.  
  808.           $_ = "I have 2 numbers: 53147";
  809.           @pats = qw{
  810.           (.*)(\d*)
  811.           (.*)(\d+)
  812.           (.*?)(\d*)
  813.           (.*?)(\d+)
  814.           (.*)(\d+)$
  815.           (.*?)(\d+)$
  816.           (.*)\b(\d+)$
  817.           (.*\D)(\d+)$
  818.           };
  819.  
  820.           for $pat (@pats) {
  821.           printf "%-12s    ", $pat;
  822.           if ( /$pat/ )    {
  823.               print "<$1> <$2>\n";
  824.           } else {
  825.               print "FAIL\n";
  826.           }
  827.           }
  828.  
  829.       That will print out:
  830.  
  831.           (.*)(\d*)       <I have 2 numbers: 53147> <>
  832.           (.*)(\d+)       <I have 2 numbers: 5314> <7>
  833.           (.*?)(\d*)   <> <>
  834.           (.*?)(\d+)   <I have > <2>
  835.           (.*)(\d+)$   <I have 2 numbers: 5314> <7>
  836.           (.*?)(\d+)$  <I have 2 numbers: >    <53147>
  837.           (.*)\b(\d+)$ <I have 2 numbers: >    <53147>
  838.           (.*\D)(\d+)$ <I have 2 numbers: >    <53147>
  839.  
  840.       As you see, this can be a bit    tricky.     It's important    to
  841.       realize that a regular expression is merely a    set of
  842.       assertions that gives    a definition of    success.  There    may be
  843.       0, 1,    or several different ways that the definition might
  844.       succeed against a particular string.    And if there are
  845.       multiple ways    it might succeed, you need to understand
  846.       backtracking to know which variety of    success    you will
  847.       achieve.
  848.  
  849.       When using lookahead assertions and negations, this can all
  850.       get even tricker.  Imagine you'd like    to find    a sequence of
  851.       non-digits not followed by "123".  You might try to write
  852.  
  853.  
  854.  
  855.      Page 13                        (printed 10/23/98)
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  863.  
  864.  
  865.  
  866.       that as
  867.  
  868.           $_ = "ABC123";
  869.           if ( /^\D*(?!123)/ ) {          # Wrong!
  870.           print    "Yup, no 123 in    $_\n";
  871.           }
  872.  
  873.       But that isn't going to match; at least, not the way you're
  874.       hoping.  It claims that there    is no 123 in the string.
  875.       Here's a clearer picture of why it that pattern matches,
  876.       contrary to popular expectations:
  877.  
  878.           $x = 'ABC123' ;
  879.           $y = 'ABC445' ;
  880.  
  881.           print "1:    got $1\n" if $x    =~ /^(ABC)(?!123)/ ;
  882.           print "2:    got $1\n" if $y    =~ /^(ABC)(?!123)/ ;
  883.  
  884.           print "3:    got $1\n" if $x    =~ /^(\D*)(?!123)/ ;
  885.           print "4:    got $1\n" if $y    =~ /^(\D*)(?!123)/ ;
  886.  
  887.       This prints
  888.  
  889.           2: got ABC
  890.           3: got AB
  891.           4: got ABC
  892.  
  893.       You might have expected test 3 to fail because it seems to a
  894.       more general purpose version of test 1.  The important
  895.       difference between them is that test 3 contains a quantifier
  896.       (\D*)    and so can use backtracking, whereas test 1 will not.
  897.       What's happening is that you've asked    "Is it true that at
  898.       the start of $x, following 0 or more non-digits, you have
  899.       something that's not 123?"  If the pattern matcher had let
  900.       \D* expand to    "ABC", this would have caused the whole
  901.       pattern to fail.  The    search engine will initially match \D*
  902.       with "ABC".  Then it will try    to match (?!123    with "123",
  903.       which    of course fails.  But because a    quantifier (\D*) has
  904.       been used in the regular expression, the search engine can
  905.       backtrack and    retry the match    differently in the hope    of
  906.       matching the complete    regular    expression.
  907.  
  908.       The pattern really, _r_e_a_l_l_y wants to succeed, so it uses the
  909.       standard pattern back-off-and-retry and lets \D* expand to
  910.       just "AB" this time.    Now there's indeed something following
  911.       "AB" that is not "123".  It's    in fact    "C123",    which
  912.       suffices.
  913.  
  914.       We can deal with this    by using both an assertion and a
  915.       negation.  We'll say that the    first part in $1 must be
  916.       followed by a    digit, and in fact, it must also be followed
  917.       by something that's not "123".  Remember that    the lookaheads
  918.  
  919.  
  920.  
  921.      Page 14                        (printed 10/23/98)
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  929.  
  930.  
  931.  
  932.       are zero-width expressions--they only    look, but don't
  933.       consume any of the string in their match.  So    rewriting this
  934.       way produces what you'd expect; that is, case    5 will fail,
  935.       but case 6 succeeds:
  936.  
  937.           print "5:    got $1\n" if $x    =~ /^(\D*)(?=\d)(?!123)/ ;
  938.           print "6:    got $1\n" if $y    =~ /^(\D*)(?=\d)(?!123)/ ;
  939.  
  940.           6: got ABC
  941.  
  942.       In other words, the two zero-width assertions    next to    each
  943.       other    work as    though they're ANDed together, just as you'd
  944.       use any builtin assertions:  /^$/ matches only if you're at
  945.       the beginning    of the line AND    the end    of the line
  946.       simultaneously.  The deeper underlying truth is that
  947.       juxtaposition    in regular expressions always means AND,
  948.       except when you write    an explicit OR using the vertical bar.
  949.       /ab/ means match "a" AND (then) match    "b", although the
  950.       attempted matches are    made at    different positions because
  951.       "a" is not a zero-width assertion, but a one-width
  952.       assertion.
  953.  
  954.       One warning: particularly complicated    regular    expressions
  955.       can take exponential time to solve due to the    immense    number
  956.       of possible ways they    can use    backtracking to    try match.
  957.       For example this will    take a very long time to run
  958.  
  959.           /((a{0,5}){0,5}){0,5}/
  960.  
  961.       And if you used *'s instead of limiting it to    0 through 5
  962.       matches, then    it would take literally    forever--or until you
  963.       ran out of stack space.
  964.  
  965.       A powerful tool for optimizing such beasts is    "independent"
  966.       groups, which    do not backtrace (see the (?>_p_a_t_t_e_r_n)
  967.       manpage).  Note also that zero-length    lookahead/lookbehind
  968.       assertions will not backtrace    to make    the tail match,    since
  969.       they are in "logical"    context: only the fact whether they
  970.       match    or not is considered relevant.    For an example where
  971.       side-effects of a lookahead _m_i_g_h_t have influenced the
  972.       following match, see the (?>_p_a_t_t_e_r_n) manpage.
  973.  
  974.       VVVVeeeerrrrssssiiiioooonnnn 8888 RRRReeeegggguuuullllaaaarrrr EEEExxxxpppprrrreeeessssssssiiiioooonnnnssss
  975.  
  976.       In case you're not familiar with the "regular" Version 8
  977.       regex    routines, here are the pattern-matching    rules not
  978.       described above.
  979.  
  980.       Any single character matches itself, unless it is a
  981.       _m_e_t_a_c_h_a_r_a_c_t_e_r    with a special meaning described here or
  982.       above.  You can cause    characters that    normally function as
  983.       metacharacters to be interpreted literally by    prefixing them
  984.  
  985.  
  986.  
  987.      Page 15                        (printed 10/23/98)
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  995.  
  996.  
  997.  
  998.       with a "\" (e.g., "\." matches a ".",    not any    character;
  999.       "\\" matches a "\").    A series of characters matches that
  1000.       series of characters in the target string, so    the pattern
  1001.       blurfl would match "blurfl" in the target string.
  1002.  
  1003.       You can specify a character class, by    enclosing a list of
  1004.       characters in    [], which will match any one character from
  1005.       the list.  If    the first character after the "[" is "^", the
  1006.       class    matches    any character not in the list.    Within a list,
  1007.       the "-" character is used to specify a range,    so that    a-z
  1008.       represents all characters between "a"    and "z", inclusive.
  1009.       If you want "-" itself to be a member    of a class, put    it at
  1010.       the start or end of the list,    or escape it with a backslash.
  1011.       (The following all specify the same class of three
  1012.       characters: [-az], [az-], and    [a\-z].     All are different
  1013.       from [a-z], which specifies a    class containing twenty-six
  1014.       characters.)
  1015.  
  1016.       Characters may be specified using a metacharacter syntax
  1017.       much like that used in C: "\n" matches a newline, "\t" a
  1018.       tab, "\r" a carriage return, "\f" a form feed, etc.  More
  1019.       generally, \_n_n_n, where _n_n_n is    a string of octal digits,
  1020.       matches the character    whose ASCII value is _n_n_n.  Similarly,
  1021.       \x_n_n,    where _n_n are hexadecimal digits, matches the character
  1022.       whose    ASCII value is _n_n. The expression \c_x matches the
  1023.       ASCII    character control-_x.  Finally, the "." metacharacter
  1024.       matches any character    except "\n" (unless you    use /s).
  1025.  
  1026.       You can specify a series of alternatives for a pattern using
  1027.       "|" to separate them,    so that    fee|fie|foe will match any of
  1028.       "fee", "fie",    or "foe" in the    target string (as would
  1029.       f(e|i|o)e).  The first alternative includes everything from
  1030.       the last pattern delimiter ("(", "[",    or the beginning of
  1031.       the pattern) up to the first "|", and    the last alternative
  1032.       contains everything from the last "|"    to the next pattern
  1033.       delimiter.  For this reason, it's common practice to include
  1034.       alternatives in parentheses, to minimize confusion about
  1035.       where    they start and end.
  1036.  
  1037.       Alternatives are tried from left to right, so    the first
  1038.       alternative found for    which the entire expression matches,
  1039.       is the one that is chosen. This means    that alternatives are
  1040.       not necessarily greedy. For example: when mathing foo|foot
  1041.       against "barefoot", only the "foo" part will match, as that
  1042.       is the first alternative tried, and it successfully matches
  1043.       the target string. (This might not seem important, but it is
  1044.       important when you are capturing matched text    using
  1045.       parentheses.)
  1046.  
  1047.       Also remember    that "|" is interpreted    as a literal within
  1048.       square brackets, so if you write [fee|fie|foe] you're    really
  1049.       only matching    [feio|].
  1050.  
  1051.  
  1052.  
  1053.      Page 16                        (printed 10/23/98)
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  1061.  
  1062.  
  1063.  
  1064.       Within a pattern, you    may designate subpatterns for later
  1065.       reference by enclosing them in parentheses, and you may
  1066.       refer    back to    the _nth    subpattern later in the    pattern    using
  1067.       the metacharacter \_n.     Subpatterns are numbered based    on the
  1068.       left to right    order of their opening parenthesis.  A
  1069.       backreference    matches    whatever actually matched the
  1070.       subpattern in    the string being examined, not the rules for
  1071.       that subpattern.  Therefore, (0|0x)\d*\s\1\d*    will match
  1072.       "0x1234 0x4321", but not "0x1234 01234", because subpattern
  1073.       1 actually matched "0x", even    though the rule    0|0x could
  1074.       potentially match the    leading    0 in the second    number.
  1075.  
  1076.       WWWWAAAARRRRNNNNIIIINNNNGGGG oooonnnn \\\\1111    vvvvssss $$$$1111
  1077.  
  1078.       Some people get too used to writing things like:
  1079.  
  1080.           $pattern =~ s/(\W)/\\\1/g;
  1081.  
  1082.       This is grandfathered    for the    RHS of a substitute to avoid
  1083.       shocking the sssseeeedddd addicts, but    it's a dirty habit to get
  1084.       into.     That's    because    in PerlThink, the righthand side of a
  1085.       s/// is a double-quoted string.  \1 in the usual double-
  1086.       quoted string    means a    control-A.  The    customary Unix meaning
  1087.       of \1    is kludged in for s///.     However, if you get into the
  1088.       habit    of doing that, you get yourself    into trouble if    you
  1089.       then add an /e modifier.
  1090.  
  1091.           s/(\d+)/ \1 + 1 /eg;      # causes warning under -w
  1092.  
  1093.       Or if    you try    to do
  1094.  
  1095.           s/(\d+)/\1000/;
  1096.  
  1097.       You can't disambiguate that by saying    \{1}000, whereas you
  1098.       can fix it with ${1}000.  Basically, the operation of
  1099.       interpolation    should not be confused with the    operation of
  1100.       matching a backreference.  Certainly they mean two different
  1101.       things on the    _l_e_f_t side of the s///.
  1102.  
  1103.       RRRReeeeppppeeeeaaaatttteeeedddd ppppaaaatttttttteeeerrrrnnnnssss mmmmaaaattttcccchhhhiiiinnnngggg zzzzeeeerrrroooo----lllleeeennnnggggtttthhhh ssssuuuubbbbssssttttrrrriiiinnnngggg
  1104.  
  1105.       WARNING: Difficult material (and prose) ahead.  This section
  1106.       needs    a rewrite.
  1107.  
  1108.       Regular expressions provide a    terse and powerful programming
  1109.       language.  As    with most other    power tools, power comes
  1110.       together with    the ability to wreak havoc.
  1111.  
  1112.       A common abuse of this power stems from the ability to make
  1113.       infinite loops using regular expressions, with something as
  1114.       innocous as:
  1115.  
  1116.  
  1117.  
  1118.  
  1119.      Page 17                        (printed 10/23/98)
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  1127.  
  1128.  
  1129.  
  1130.           'foo' =~ m{ ( o? )* }x;
  1131.  
  1132.       The o? can match at the beginning of 'foo', and since    the
  1133.       position in the string is not    moved by the match, o? would
  1134.       match    again and again    due to the * modifier.    Another    common
  1135.       way to create    a similar cycle    is with    the looping modifier
  1136.       //g:
  1137.  
  1138.           @matches = ( 'foo' =~ m{ o? }xg );
  1139.  
  1140.       or
  1141.  
  1142.           print "match: <$&>\n" while 'foo'    =~ m{ o? }xg;
  1143.  
  1144.       or the loop implied by _s_p_l_i_t().
  1145.  
  1146.       However, long    experience has shown that many programming
  1147.       tasks    may be significantly simplified    by using repeated
  1148.       subexpressions which may match zero-length substrings, with
  1149.       a simple example being:
  1150.  
  1151.           @chars = split //, $string;        # // is not    magic in split
  1152.           ($whitewashed = $string) =~ s/()/    /g; # parens avoid magic s// /
  1153.  
  1154.       Thus Perl allows the /()/ construct, which _f_o_r_c_e_f_u_l_l_y    _b_r_e_a_k_s
  1155.       _t_h_e _i_n_f_i_n_i_t_e _l_o_o_p.  The rules    for this are different for
  1156.       lower-level loops given by the greedy    modifiers *+{},    and
  1157.       for higher-level ones    like the /g modifier or    _s_p_l_i_t()
  1158.       operator.
  1159.  
  1160.       The lower-level loops    are _i_n_t_e_r_r_u_p_t_e_d    when it    is detected
  1161.       that a repeated expression did match a zero-length
  1162.       substring, thus
  1163.  
  1164.          m{    (?: NON_ZERO_LENGTH | ZERO_LENGTH )* }x;
  1165.  
  1166.       is made equivalent to
  1167.  
  1168.          m{      (?: NON_ZERO_LENGTH )*
  1169.         |
  1170.           (?: ZERO_LENGTH )?
  1171.           }x;
  1172.  
  1173.       The higher level-loops preserve an additional    state between
  1174.       iterations:  whether the last    match was zero-length.    To
  1175.       break    the loop, the following    match after a zero-length
  1176.       match    is prohibited to have a    length of zero.     This
  1177.       prohibition interacts    with backtracking (see the section on
  1178.       _B_a_c_k_t_r_a_c_k_i_n_g), and so    the _s_e_c_o_n_d _b_e_s_t    match is chosen    if the
  1179.       _b_e_s_t match is    of zero    length.
  1180.  
  1181.       Say,
  1182.  
  1183.  
  1184.  
  1185.      Page 18                        (printed 10/23/98)
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  1193.  
  1194.  
  1195.  
  1196.           $_ = 'bar';
  1197.           s/\w??/<$&>/g;
  1198.  
  1199.       results in "<<b><><a><><r><>">.  At each position of the
  1200.       string the best match    given by non-greedy ?? is the zero-
  1201.       length match,    and the    _s_e_c_o_n_d _b_e_s_t match is what is matched
  1202.       by \w.  Thus zero-length matches alternate with one-
  1203.       character-long matches.
  1204.  
  1205.       Similarly, for repeated m/()/g the second-best match is the
  1206.       match    at the position    one notch further in the string.
  1207.  
  1208.       The additional state of being    _m_a_t_c_h_e_d    _w_i_t_h _z_e_r_o-_l_e_n_g_t_h is
  1209.       associated to    the matched string, and    is reset by each
  1210.       assignment to    _p_o_s().
  1211.  
  1212.       CCCCrrrreeeeaaaattttiiiinnnngggg ccccuuuussssttttoooommmm RRRREEEE eeeennnnggggiiiinnnneeeessss
  1213.  
  1214.       Overloaded constants (see the    _o_v_e_r_l_o_a_d manpage) provide a
  1215.       simple way to    extend the functionality of the    RE engine.
  1216.  
  1217.       Suppose that we want to enable a new RE escape-sequence \Y|
  1218.       which    matches    at boundary between white-space    characters and
  1219.       non-whitespace characters.  Note that
  1220.       (?=\S)(?<!\S)|(?!\S)(?<=\S) matches exactly at these
  1221.       positions, so    we want    to have    each \Y| in the    place of the
  1222.       more complicated version.  We    can create a module customre
  1223.       to do    this:
  1224.  
  1225.           package customre;
  1226.           use overload;
  1227.  
  1228.           sub import {
  1229.         shift;
  1230.         die "No    argument to customre::import allowed" if @_;
  1231.         overload::constant 'qr'    => \&convert;
  1232.           }
  1233.  
  1234.           sub invalid { die    "/$_[0]/: invalid escape '\\$_[1]'"}
  1235.  
  1236.           my %rules    = ( '\\' => '\\',
  1237.                 'Y|' => qr/(?=\S)(?<!\S)|(?!\S)(?<=\S)/ );
  1238.           sub convert {
  1239.         my $re = shift;
  1240.         $re =~ s{
  1241.               \\ ( \\ | Y .    )
  1242.             }
  1243.             { $rules{$1} or    invalid($re,$1)    }sgex;
  1244.         return $re;
  1245.           }
  1246.  
  1247.       Now use customre enables the new escape in constant regular
  1248.  
  1249.  
  1250.  
  1251.      Page 19                        (printed 10/23/98)
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  1259.  
  1260.  
  1261.  
  1262.       expressions, i.e., those without any runtime variable
  1263.       interpolations.  As documented in the    _o_v_e_r_l_o_a_d manpage, this
  1264.       conversion will work only over literal parts of regular
  1265.       expressions.    For \Y|$re\Y| the variable part    of this
  1266.       regular expression needs to be converted explicitly (but
  1267.       only if the special meaning of \Y| should be enabled inside
  1268.       $re):
  1269.  
  1270.           use customre;
  1271.           $re = <>;
  1272.           chomp $re;
  1273.           $re = customre::convert $re;
  1274.           /\Y|$re\Y|/;
  1275.  
  1276.  
  1277.       SSSSEEEEEEEE AAAALLLLSSSSOOOO
  1278.  
  1279.       the section on _R_e_g_e_x_p    _Q_u_o_t_e-_L_i_k_e _O_p_e_r_a_t_o_r_s in    the _p_e_r_l_o_p
  1280.       manpage.
  1281.  
  1282.       the section on _G_o_r_y _d_e_t_a_i_l_s _o_f _p_a_r_s_i_n_g _q_u_o_t_e_d    _c_o_n_s_t_r_u_c_t_s in
  1283.       the _p_e_r_l_o_p manpage.
  1284.  
  1285.       the pos entry    in the _p_e_r_l_f_u_n_c    manpage.
  1286.  
  1287.       the _p_e_r_l_l_o_c_a_l_e manpage.
  1288.  
  1289.       _M_a_s_t_e_r_i_n_g _R_e_g_u_l_a_r _E_x_p_r_e_s_s_i_o_n_s    (see the _p_e_r_l_b_o_o_k manpage) by
  1290.       Jeffrey Friedl.
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.      Page 20                        (printed 10/23/98)
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))          7777////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.      Page 21                        (printed 10/23/98)
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.